/**
 * Copyright (c) 2015-2017, Henry Yang 杨勇 (gismail@foxmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mapfinal.server.mgrdb.service.impl;

import java.util.List;

import com.jfinal.kit.StrKit;
import com.lambkit.common.service.LambkitModelServiceImpl;
import com.lambkit.core.aop.AopKit;
import com.lambkit.db.meta.ColumnMeta;
import com.lambkit.db.mgr.IField;
import com.lambkit.db.mgr.ITable;
import com.lambkit.db.mgr.MgrConstants;
import com.lambkit.db.sql.column.Columns;
import com.mapfinal.server.mgrdb.MfTableFieldService;
import com.mapfinal.server.mgrdb.model.MfTable;
import com.mapfinal.server.mgrdb.model.MfTableField;

/**
 * @author yangyong 
 * @website: www.lambkit.com
 * @email: gismail@foxmail.com
 * @date 2021-09-06
 * @version 1.0
 * @since 1.0
 */
public class MfTableFieldServiceImpl extends LambkitModelServiceImpl<MfTableField> implements MfTableFieldService {
	
	private MfTableField DAO = null;
	
	public MfTableField dao() {
		if(DAO==null) {
			DAO = AopKit.singleton(MfTableField.class);
		}
		return DAO;
	}

	@Override
	public String getPkey(Object tbid) {
		// TODO Auto-generated method stub
		List<MfTableField> fields = find(Columns.create("tbid", tbid).eq("iskey", "Y"));
		if(fields.size() < 1) return null;
		StringBuilder sb = new StringBuilder();
		for (MfTableField fld : fields) {
			sb.append(",").append(fld.getName());
		}
		return sb.substring(1);
	}

	@Override
	public int deleteByTbid(Object tbid) {
		// TODO Auto-generated method stub
		return delete(Columns.create("tbid", tbid));
	}

	@Override
	public List<MfTableField> findByTbid(Object tbid) {
		// TODO Auto-generated method stub
		return find(Columns.create("tbid", tbid));
	}

	@Override
	public List<MfTableField> findByTbid(Object tbid, int type) {
		// TODO Auto-generated method stub
		return findByTbid(tbid, type, "orders, fldid");
	}

	@Override
	public List<MfTableField> findByTbid(Object tbid, String orderby) {
		// TODO Auto-generated method stub
		return findByTbid(tbid, MgrConstants.NONE, orderby);
	}

	@Override
	public List<MfTableField> findByTbid(Object tbid, int type, String orderby) {
		// TODO Auto-generated method stub
		if(StrKit.isBlank(orderby)) orderby = "orders, fldid";
		return find(Columns.create("tbid", tbid), orderby);
	}

	@Override
	public MfTableField findById(Object fldid, int type) {
		// TODO Auto-generated method stub
		return findById(fldid);
	}

	@Override
	public MfTableField findFirstByTbName(String tbname, String fld, Object value) {
		// TODO Auto-generated method stub
		return findFirstByTbName(tbname, MgrConstants.NONE, fld, value);
	}

	@Override
	public MfTableField findFirstByTbName(String tbname, int type, String fld, Object value) {
		// TODO Auto-generated method stub
		MfTable table = MfTable.service().findByName(tbname);
		if(table!=null) {
			return findFirst(Columns.create(fld, value).eq("tbid", table.getTbid()));
		}
		return null;
	}
	
	@Override
	public IField findFirstByTbidAndName(Object tbid, int type, String fldname) {
		// TODO Auto-generated method stub
		MfTable table = MfTable.service().findById(tbid);
		if(table!=null) {
			return findFirst(Columns.create("name", fldname).eq("tbid", table.getId()));
		}
		return null;
	}

	@Override
	public MfTableField findFirstByTbid(Object tbid, String fld, Object value) {
		// TODO Auto-generated method stub
		return findFirstByTbid(tbid, MgrConstants.NONE, fld, value);
	}

	@Override
	public MfTableField findFirstByTbid(Object tbid, int type, String fld, Object value) {
		// TODO Auto-generated method stub
		MfTable table = MfTable.service().findById(tbid);
		if(table!=null) {
			return findFirst(Columns.create(fld, value).eq("tbid", table.getId()));
		}
		return null;
	}

	@Override
	public MfTableField findGeomField(Object tbid, int type) {
		// TODO Auto-generated method stub
		MfTable table = MfTable.service().findById(tbid);
		if(table!=null) {
			return findFirst(Columns.create().like("geo_type", "geom%"));
		}
		return null;
	}

	@Override
	public MfTableField getAddOrEditModel(Object id, int type) {
		// TODO Auto-generated method stub
		MfTableField field = findById(id);
		if(field==null) {
			field = new MfTableField();
			field.setStatus(0);
		}
		return field;
	}

	@Override
	public MfTableField columnToField(ColumnMeta column, ITable tbc) {
		// TODO Auto-generated method stub
		if(column!=null) {
			String tbname = tbc.getName();
			String name = column.getName();
			if(StrKit.notBlank(tbname) && StrKit.notBlank(tbname)) {
				MfTableField model = findFirstByTbName(tbname, MgrConstants.NONE, "name", name);
				if(model!=null) {
					model.setDatatype(column.getType());
					model.setIskey(column.isPrimaryKey() ? "Y" : "N");
					model.setIsunsigned(column.isSigned() ? "N" : "Y");
					model.setIsai(column.isAutoInctement() ? "Y" : "N");
					model.setIsnullable(column.getIsNullableType()==1 ? "Y" : "N");
					model.setJtype(column.getJavaType());
					if(StrKit.isBlank(model.getAlias())) model.setAlias(column.getAttrName());
					model.update();
				}
				return model;
			}
		}
		MfTableField model = getAddOrEditModel(-1, MgrConstants.NONE);
		if(column.isPrimaryKey()) {
			model.setIskey("Y");
		} else {
			model.setIskey("N");
		}
		model.setTbid((Integer)tbc.getId());
		model.setName(column.getName());
		model.setTitle(column.getTitle());
		String datatype = column.getType().toLowerCase();
		if(datatype.contains("char") ||
				datatype.contains("number") ||
				datatype.contains("numeric") ||
				datatype.contains("decimal")){
			if(column.getPrecision() > 0) {
				datatype += "(" + column.getPrecision();
				if(column.getScale() > 0) datatype += "," + column.getScale();
				datatype += ")";
			}
		}
		model.setDatatype(datatype);
		model.setIsunsigned("N");
		if(column.isAutoInctement()) model.setIsai("Y");
		else model.setIsai("N");
		if(column.getIsNullableType() == 1) model.setIsnullable("Y");
		else model.setIsnullable("N");
		//model.setDescript(column.getRemarks());
		//model.setIsfk("N");
		if(column.isPrimaryKey()) {
			model.setIsview("Y");
			model.setIsedit("N");
			model.setIsselect("Y");
			model.setIsmustfld("N");
		} else if("blob".equalsIgnoreCase(column.getType())) {
			model.setIsview("N");
			model.setIsedit("N");
			model.setIsselect("N");
			model.setIsmustfld("N");
		} else {
			model.setIsview("Y");
			model.setIsedit("Y");
			model.setIsselect("Y");
			model.setIsmustfld("Y");
		}
		model.save();
		return model;
	}

	
}
